package com.karaku.karaku_collector.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.karaku.karaku_collector.LocalSetting;
import com.karaku.karaku_collector.R;
import com.karaku.karaku_collector.collector.model.VideoProfile;
import com.karaku.karaku_collector.common.binder.BindView;
import com.karaku.karaku_collector.common.binder.UIBinder;
import com.karaku.karaku_collector.util.Size;
import com.karaku.karaku_collector.util.actions.Action1;

/**
 * Created by guoping on 2017/11/7.
 */

@BindView(R.layout.view_profile_selector)
public class ProfileSelectorView extends FrameLayout {

    @BindView(R.id.lv_profiles)
    private ListView lvProfiles;

    private Action1<Integer> callback;
    private Adapter adapter;

    public ProfileSelectorView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        UIBinder.bind(this, this, true);
        adapter = new Adapter();
        lvProfiles.setAdapter(adapter);
        lvProfiles.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int profile = VideoProfile.getProfiles().get(position).profile;
                if (callback != null) {
                    callback.run(profile);
                }
                hide();
            }
        });
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                hide();
            }
        });
    }

    public void show(final Action1<Integer> callback) {
        this.callback = callback;
        setVisibility(VISIBLE);
        adapter.notifyDataSetChanged();
    }

    public void hide() {
        this.callback = null;
        setVisibility(GONE);
    }


    class Adapter extends BaseAdapter {
        @Override
        public int getCount() {
            return VideoProfile.getProfiles().size();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Object getItem(int position) {
            return VideoProfile.getProfiles().get(position);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = new ViewHolder().rootView;
            }
            ViewHolder vh = (ViewHolder) convertView.getTag();
            VideoProfile p = VideoProfile.getProfiles().get(position);
            vh.tvName.setText(p.name);
            vh.tvSize.setText(p.size);
            vh.tvFrameRate.setText(String.valueOf(p.frameRate));
            vh.tvBitrate.setText(String.valueOf(p.bitrate));

            int lp = LocalSetting.getInstance().getVideoProfile();
            if (lp == p.profile) {
                convertView.setBackgroundColor(0XFFEEEEEE);
            } else {
                convertView.setBackgroundResource(R.drawable.clickable_bg);
            }
            return convertView;
        }
    }

    @BindView(R.layout.item_profile)
    static class ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_size)
        TextView tvSize;
        @BindView(R.id.tv_frame_rate)
        TextView tvFrameRate;
        @BindView(R.id.tv_bitrate)
        TextView tvBitrate;

        private View rootView;
        ViewHolder() {
            rootView = UIBinder.bind(this);
            rootView.setTag(this);
        }
    }
}
